package passworks;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import passworks.Gates.*;

public class Main {

    public static void main(String[] args) {

        System.out.println("PassWorks - Szkeleton");

        boolean loop = true;
        while(loop) {
            loop = false;

            /*
             * Menu kiirasa
             */

            System.out.println();
            System.out.println("1 - Halozat felepitese");
            System.out.println("2 - Visszacsatolas nelkuli halozat");
            System.out.println("3 - Visszacsatolt stabil halozat");
            System.out.println("4 - Visszacsatolt instabil halozat");
            System.out.println("5 - utemezo leptetese");
            System.out.println("6 - Kapcsolo atallitasa");
            System.out.println("7 - Jelgenerator leptetese");
            System.out.println("8 - Halozat alaphelyzetbe allitasa");
            System.out.println();
            System.out.println("Valassz menupontot");

            /*
             * Beolvasas
             */
            BufferedReader keyboard;
            keyboard = new BufferedReader(
                    new InputStreamReader(System.in));

            try {
                int index = Integer.parseInt(keyboard.readLine());
                switch(index) {
                    case 1:
                        UseCase1();
                        break;
                    case 2:
                        UseCase2();
                        break;
                    case 3:
                        UseCase3();
                        break;
                    case 4:
                        UseCase4();
                        break;
                    case 5:
                        UseCase5();
                        break;
                    case 6:
                        UseCase6();
                        break;
                    case 7:
                        UseCase7();
                        break;
                    case 8:
                        UseCase8();
                        break;
                    default:
                        System.out.println("Hibas menupont!");
                }
            } catch (IOException e) {
                System.out.println(e.getClass());

            }

            System.out.println("Szeretnel meg egy use-case-t tesztelni? (I/N)");
            try {
                if(keyboard.readLine().compareTo("I") == 0) {
                    loop = true;
                    /*
                     * Szamlalok 0-ra allitasa
                     */
                    AND.ID = 0;
                    Generator.ID = 0;
                    LED.ID = 0;
                    NOT.ID = 0;
                    Node.ID = 0;
                    OR.ID = 0;
                    Switch.ID = 0;
                }
            } catch (Exception e) {}
        }
    }

    static void UseCase1() {
        /*
         * Halozat felepitese
         */

        /*
         * Elokeszites
         */
        Logger.SetSilent(true);
        Model model = new Model();

        /*
         * Tenyleges fuggvenyhivasok
         */
        Logger.SetSilent(false);
        model.CreateCircuit();
    }

    static void UseCase2() {
        /*
         * Visszacsatolas nelkuli halozat
         */

        /*
         * Elokeszites
         */
        Logger.SetSilent(true);

        Model model = new Model();

        //Lista amit a generator leptet.
        List<Boolean> pattern = new ArrayList<Boolean>();
        pattern.add(Boolean.TRUE);
        pattern.add(Boolean.FALSE);

        Generator generator = new Generator(pattern);
        model.gates.add(generator);
        model.sources.add(generator);

        NOT not = new NOT();
        model.gates.add(not);

        Wire wire0 = new Wire(not);
        model.wires.add(wire0);
        generator.AddOutput(wire0);
        not.AddInput(wire0);

        LED led = new LED();
        model.gates.add(led);

        Wire wire1 = new Wire(led);
        model.wires.add(wire1);
        not.AddOutput(wire1);
        led.AddInput(wire1);

        /*
         * Tenyleges fuggvenyhivasok
         */
        Logger.SetSilent(false);
        model.Simulate();
        
        
    }

    static void UseCase3() {
        /*
         * Visszacsatolt stabil halozat
         */
        
        /*
         * Elokeszites
         */
        Logger.SetSilent(true);

        Model model = new Model();
        
        //Lista amit a generator leptet.
        List<Boolean> pattern = new ArrayList<Boolean>();
        pattern.add(Boolean.TRUE);
        pattern.add(Boolean.FALSE);

        Generator generator = new Generator(pattern);
        model.gates.add(generator);
        model.sources.add(generator);

        OR or = new OR();
        model.gates.add(or);

        Wire wire0 = new Wire(or);
        model.wires.add(wire0);
        generator.AddOutput(wire0);
        or.AddInput(wire0);

        Wire wire1 = new Wire(or);
        model.wires.add(wire1);
        or.AddInput(wire1);
        or.AddOutput(wire1);

        /*
         * Tenyleges fuggvenyhivasok
         */
        Logger.SetSilent(false);
        model.Simulate();
    }

    static void UseCase4() {
        /*
         * Visszacsatolt instabil halozat
         */
        
        /*
         * Elokeszites
         */
        Logger.SetSilent(true);

        Model model = new Model();

        Switch switch0 = new Switch(false);
        model.sources.add(switch0);
        model.gates.add(switch0);
        
        AND and = new AND();
        model.gates.add(and);

        Wire wire1 = new Wire(and);
        model.wires.add(wire1);

        switch0.AddOutput(wire1);
        and.AddInput(wire1);

        NOT not = new NOT();
        model.gates.add(not);

        Wire wire2 = new Wire(not);
        model.wires.add(wire2);

        and.AddOutput(wire2);
        not.AddInput(wire2);

        Wire wire3 = new Wire(and);
        model.wires.add(wire3);

        and.AddInput(wire3);
        not.AddOutput(wire3);

        /*
         * Tenyleges fuggvenyhivasok
         */
        Logger.SetSilent(false);
        model.Simulate();

    }

    static void UseCase5() {
        /*
         * utemezo leptetese
         */
        
        /*
         * Elokeszites
         */
        Logger.SetSilent(true);

        Model model = new Model();

        Switch switch0 = new Switch(false);
        model.sources.add(switch0);
        model.gates.add(switch0);
        
        LED led = new LED();
        model.gates.add(led);

        Wire wire = new Wire(led);
        model.wires.add(wire);

        switch0.AddOutput(wire);
        led.AddInput(wire);


        /*
         * Tenyleges fuggvenyhivasok
         */
        Logger.SetSilent(false);
        boolean makeStep = true;

        while(makeStep) {
        model.Simulate();
        makeStep = Logger.Read("Szeretnel meg egy lepest?");
        }


    }

    static void UseCase6() {
        /*
         * Kapcsolo atallitasa
         */

        /*
         * Elokeszites
         */
        Logger.SetSilent(true);
        Switch switch0 = new Switch(false);

        /*
         * Tenyleges fuggvenyhivasok
         */
        Logger.SetSilent(false);
        switch0.SetState(true);

    }

    static void UseCase7() {
        /*
         * Jelgenerator leptetese
         */

        /*
         * Elokeszites
         */
        Logger.SetSilent(true);

        Model model = new Model();

        Generator gen = new Generator(new ArrayList<Boolean>());
        model.sources.add(gen);
        model.gates.add(gen);

        AND and = new AND();
        model.gates.add(and);

        Wire wire0 = new Wire(and);
        model.wires.add(wire0);

        gen.AddOutput(wire0);
        and.AddInput(wire0);

        Switch switch0 = new Switch(false);
        model.sources.add(switch0);
        model.gates.add(switch0);       

        Wire wire1 = new Wire(and);
        model.wires.add(wire1);

        switch0.AddOutput(wire1);
        and.AddInput(wire1);

        LED led = new LED();
        model.gates.add(led);

        Wire wire2 = new Wire(led);
        model.wires.add(wire2);

        and.AddOutput(wire2);
        led.AddInput(wire2);

        /*
         * Tenyleges fuggvenyhivasok
         */
        Logger.SetSilent(false);
        model.Simulate();
    }

    static void UseCase8() {
        /*
         * Halozat alaphelyzetbe allitasa
         */

        /*
         * Elokeszites
         */
        Logger.SetSilent(true);

        Model model = new Model();

        //Lista amit a generator leptet.
        List<Boolean> pattern = new ArrayList<Boolean>();
        pattern.add(Boolean.TRUE);
        pattern.add(Boolean.FALSE);

        Generator generator = new Generator(pattern);
        model.gates.add(generator);
        model.sources.add(generator);

        NOT not = new NOT();
        model.gates.add(not);

        Wire wire0 = new Wire(not);
        model.wires.add(wire0);
        generator.AddOutput(wire0);
        not.AddInput(wire0);

        LED led = new LED();
        model.gates.add(led);

        Wire wire1 = new Wire(led);
        model.wires.add(wire1);
        not.AddOutput(wire1);
        led.AddInput(wire1);

        /*
         * Tenyleges fuggvenyhivasok
         */
        Logger.SetSilent(false);
        model.ResetCircuit();
    }

}
